<?php
/**
 * Product:     One-Page Checkout Booster
 * Package:     Gold_Gonepage_1.1.1_94708
 * Purchase ID: 9CBm2i1g9YwZZ6QK4IjUneSwxlnJsImwioL1MUWAXd
 * Generated:   2011-07-15 10:07:59
 * File path:   app/code/local/Gold/Gonepage/Model/Observer.php
 * Copyright:   (c) 2011 GOLD, Inc.
 */
class Gold_Gonepage_Model_Observer
{
    public function onPredispatchCheckoutOnepageIndex(Varien_Event_Observer $observer)
    {
        if(Mage::helper('gonepage')->isDisabled())return;
        $helper = Mage::helper('gonepage');
        $checkoutUrl = Mage::getUrl($helper->getCheckoutUrl(), array('_secure'=>true)); 
        $observer->getEvent()->getControllerAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);    
        $observer->getEvent()->getControllerAction()->getResponse()->setRedirect($checkoutUrl);     
    }     
    
    public function onPredispatchCheckoutCartIndex(Varien_Event_Observer $observer)
    {
        if( Mage::helper('gonepage')->isDisabled()) return;
        
        $helper = Mage::helper('gonepage');
        $cartUrl = Mage::getUrl($helper->getCartUrl(), array('_secure'=>true));
        if ($helper->isShowCartInCheckout())
        {
            $observer->getEvent()->getControllerAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
            $observer->getEvent()->getControllerAction()->getResponse()->setRedirect($cartUrl);            
        }
    }     
  
    private function _checkRule()
    {
        /* */
        $iStoreId = Mage::app()->getStore()->getId();
        $iSiteId  = Mage::app()->getWebsite()->getId();
        $performer = Gold_Goldsys_Abstract_Service::get()->platform()->getModule('Gold_Gonepage')->getLicense()->getPerformer();
        $ruler     = $performer->getRuler();
        if (!($ruler->checkRule('store', $iStoreId, 'store') || $ruler->checkRule('store', $iSiteId, 'website')))
        {
            return false;
        }
        /* */
    
        return true;
    }
	
    public function onOnepageSaveOrderAfter(Varien_Event_Observer $observer)
    {
        $request = Mage::app()->getFrontController()->getRequest();
        $quote = $observer->getQuote();
        $helper = Mage::helper('gonepage');
        Mage::getSingleton('checkout/session')->setConfirmSameAsBilling(0);
        if ($request->getPost('newsletter') && $quote->getBillingAddress()->getEmail()) 
        {
            $session            = Mage::getSingleton('core/session');
            $customerSession    = Mage::getSingleton('customer/session');
            $email              = $quote->getBillingAddress()->getEmail();     

            $ownerId = Mage::getModel('customer/customer')
                    ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
                    ->loadByEmail($email)
                    ->getId();
            if ($ownerId !== null && $ownerId != $customerSession->getId()) 
            {
                $session->addError($helper->__('There was a problem with the newsletter subscription: This email address is already assigned to another user.'));
            } else {
                $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
                if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) 
                {
                    $session->addSuccess($helper->__('Confirmation request for newsletter subscription has been sent.'));
                }
                else {
                    $session->addSuccess($helper->__('Thank you for your newsletter subscription.'));
                }        
            }
        }    
    } 
    
    function onSalesOrderPlaceBefore(Varien_Event_Observer $observer) 
    {
        $customer = $observer->getOrder()->getCustomer();
        //$checkoutMethod =  $observer->getOrder()->getQuote()->getData('checkout_method');
        $checkoutMethod =  $observer->getQuote()->getData('checkout_method');
        $password = $customer->getPassword();
        if( ($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER)
                || $checkoutMethod == 'register') 
        {
            if ($password=== NULL || $password == '') {
                Mage::throwException( Mage::helper('customer')->__('The password cannot be empty.') );
            }        
        }
    }
}